﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Interfaces.IPresenters;
using Core.Unity;
using Microsoft.Practices.Unity;
namespace Core.Presenters
{
    class OwnedBuildingPresenter
    {
        private IOwnedBuilding _ownedBuilding;
        private IOwnedBuildingView _ownedBuildingView;
        private IOwnedBuildingDAO _ownedBuildingDao;
        private ICustomerDAO _customerDao;
        private ICustomer _customer;
        public OwnedBuildingPresenter(IOwnedBuildingView view, IOwnedBuilding ownedBuilding = null)
        {
            _ownedBuilding = ownedBuilding ?? ResolverContainer.Resolver.Resolve<IOwnedBuilding>();
            _ownedBuildingView = view;
            _customerDao = ResolverContainer.Resolver.Resolve<ICustomerDAO>();
            _ownedBuildingDao = ResolverContainer.Resolver.Resolve<IOwnedBuildingDAO>();
            _ownedBuildingView.OwnedBuilding.DataSource = _ownedBuilding;
            _ownedBuildingView.Customer.DataSource = _customerDao.GetAll(false);
            _ownedBuildingView.SaveOwnedBuilding += Save;
            _ownedBuildingView.ShowDialog();
        }

        private void Save(object sender, EventArgs eventArgs)
        {
            _ownedBuilding.Customer = (ICustomer) _ownedBuildingView.Customer.Current;
            if (_ownedBuildingDao.SaveOrUpdateAndCommit(_ownedBuilding))
            {
                _ownedBuildingView.Close();
            }
        }
    }
}
